#include "Widget.h"
#include "TipsWidget.h"

#include <QPushButton>
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setFixedSize(400, 250);

    QPushButton *pUpBtn = new QPushButton("Show Tips At Up", this);
    QPushButton *pDownBtn = new QPushButton("Show Tips At Down", this);
    QPushButton *pLeftBtn = new QPushButton("Show Tips At Left", this);
    QPushButton *pRightBtn = new QPushButton("Show Tips At Right", this);

    _lineEdit = new QLineEdit(this);
    _lineEdit->installEventFilter(this);

    QVBoxLayout *pLayout = new QVBoxLayout(this);
    pLayout->addWidget(pUpBtn);
    pLayout->addWidget(pDownBtn);
    pLayout->addWidget(pLeftBtn);
    pLayout->addWidget(pRightBtn);
    pLayout->addWidget(_lineEdit);

    connect(pUpBtn, SIGNAL(clicked(bool)), this, SLOT(showTipsAtUp()));
    connect(pDownBtn, SIGNAL(clicked(bool)), this, SLOT(showTipsAtDown()));
    connect(pLeftBtn, SIGNAL(clicked(bool)), this, SLOT(showTipsAtLeft()));
    connect(pRightBtn, SIGNAL(clicked(bool)), this, SLOT(showTipsAtRight()));
}

Widget::~Widget()
{
}

void Widget::showTipsAtUp()
{
    TipsWidget *pWidget = new TipsWidget("你好！\n我是 Mikasoi！\n欢迎观看我的视频，一键三连！！！", (QWidget*)sender(), D_UP);
    pWidget->show();
}

void Widget::showTipsAtDown()
{
    TipsWidget *pWidget = new TipsWidget("轻轻的我走了，\n正如我轻轻的来；\n我轻轻的招手，\n作别西天的云彩。", (QWidget*)sender(), D_DOWN);
    pWidget->setSize(160, 120);
    pWidget->setBgColor(Qt::cyan);
    pWidget->show();
}

void Widget::showTipsAtLeft()
{
    TipsWidget *pWidget = new TipsWidget("我们是一群候鸟，\n飞进了冬天的牢宠；\n在绿色的拂晓，\n去天涯远征。", (QWidget*)sender(), D_LEFT);
    pWidget->setSize(180, 120);
    pWidget->setTextColor(Qt::green);
    pWidget->setBgColor(QColorConstants::Svg::orange);
    pWidget->show();
}

void Widget::showTipsAtRight()
{
    TipsWidget *pWidget = new TipsWidget("从明天起, 做一个幸福的人\n喂马, 劈柴, 周游世界\n从明天起, 关心粮食和蔬菜\n我有一所房子, 面朝大海, 春暖花开", (QWidget*)sender(), D_RIGHT);
    pWidget->setSize(300, 122);
    pWidget->setTextColor(Qt::white);
    pWidget->setBgColor(QColor(33, 33, 33));
    pWidget->show();
}

//事件过滤器
bool Widget::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == _lineEdit)
    {
        if (event->type() == QEvent::HoverEnter)
        {
            static TipsWidget *pWidget = NULL;
            if (!pWidget)
            {
                pWidget = new TipsWidget("请输入密码，必须包含字母和数字", _lineEdit, D_UP);
                pWidget->setTextColor(Qt::red);
                pWidget->setFont(QFont("微软雅黑", 10));
                pWidget->setSize(230, 55);
            }

            if (pWidget->isHidden())
                pWidget->show();
            return true;
        }
    }
    return QWidget::eventFilter(obj,event);
}

